package com.bbz.core.map;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * HashMap四种遍历方式性能比较 分别对四种遍历方式进行10W次迭代，比较用时。 比较结果：迭代器，entrySet迭代最快
 * 
 * @author binbin.a.zhang
 *
 */
public class TestMap {
	public static void main(String[] args) {
		TestMap a = new TestMap();
		a.paixu();
		a.diedai();
		a.treeMapPaiXu();
	}

	/**
	 * 迭代性能测试
	 */
	public void diedai() {
		// 初始化，10W次赋值
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (int i = 0; i < 1000000; i++)
			map.put(i, i);

		/** 增强for循环，keySet迭代 **/
		long start = System.currentTimeMillis();
		for (Integer key : map.keySet()) {
			map.get(key);
		}
		long end = System.currentTimeMillis();
		System.out.println("增强for循环，keySet迭代 -> " + (end - start) + " ms");

		/** 增强for循环，entrySet迭代 */
		start = System.currentTimeMillis();
		for (Entry<Integer, Integer> entry : map.entrySet()) {
			entry.getKey();
			entry.getValue();
		}
		end = System.currentTimeMillis();
		System.out.println("增强for循环，entrySet迭代 -> " + (end - start) + " ms");

		/** 迭代器，keySet迭代 **/
		start = System.currentTimeMillis();
		Iterator<Integer> iterator = map.keySet().iterator();
		Integer key;
		while (iterator.hasNext()) {
			key = iterator.next();
			map.get(key);
		}
		end = System.currentTimeMillis();
		System.out.println("迭代器，keySet迭代 -> " + (end - start) + " ms");

		/** 迭代器，entrySet迭代 **/
		start = System.currentTimeMillis();
		Iterator<Map.Entry<Integer, Integer>> iterator1 = map.entrySet().iterator();
		Map.Entry<Integer, Integer> entry;
		while (iterator1.hasNext()) {
			entry = iterator1.next();
			entry.getKey();
			entry.getValue();
		}
		end = System.currentTimeMillis();
		System.out.println("迭代器，entrySet迭代 -> " + (end - start) + " ms");
	}

	/**
	 * Map排序Demo
	 */
	public void paixu() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("b", "b");
		map.put("a", "c");
		map.put("c", "a");

		// 通过ArrayList构造函数把map.entrySet()转换成list
		List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
		// 通过比较器实现比较排序
		Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
			@Override
			public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) {
				return mapping1.getKey().compareTo(mapping2.getKey());
			}
		});

		for (Map.Entry<String, String> mapping : list) {
			System.out.println(mapping.getKey() + " ：" + mapping.getValue());
		}
	}

	/**
	 * TreeMap 排序
	 */
	public void treeMapPaiXu() {
		Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				// 降序排序
				return o1.compareTo(o2);
			}
		});

		map.put("b", "b");
		map.put("a", "c");
		map.put("c", "a");
		for (String key : map.keySet()) {
			System.out.println(key + " ：" + map.get(key));
		}
	}

}
